package com.seo.controller.user.proxy;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.seo.pojo.ReturnBody;
import com.seo.resolver.DefiniteException;
import com.seo.service.user.sdk.UserService;
import com.seo.utils.Jurisdiction;

import lombok.extern.slf4j.Slf4j;

/**
 * menucontroller <br/>
 * 
 * @author shibeilei
 * @version 2017.11.30 12:20
 */
@Slf4j
@Controller
@RequestMapping(value = "/user/proxy")
public class ProxyUserController {

	@Autowired
	private UserService userService;

	private String menuUrl = "user";


	@RequestMapping("/price")
	public String price(Integer userId, HttpServletRequest request, HttpSession session) {
		log.info("编辑代理价...");
		if (!Jurisdiction.qx(menuUrl, "edit")) {
			throw new DefiniteException("权限不足");
		}
		
		Map<String, Object> user = userService.getUserById(userId);
		request.setAttribute("user", user);
		return "user/proxy/price";

	}

	@RequestMapping("/setPrice")
	@ResponseBody
	public ReturnBody setPrice(@RequestParam Map<String, Object> params) {
		log.info("保存代理价...");
		params.put("type", "00");
		return userService.updateProxyPrice(params);
	}
	
	@RequestMapping("/proxyPrices")
	public String proxyPrices(@RequestParam Map<String, Object> params, HttpServletRequest request) {
		log.info("初始化代理价格...");
		if (!Jurisdiction.qx(menuUrl, "edit")) {
			throw new DefiniteException("权限不足");
		}
		if(params == null || params.isEmpty()) {
			throw new DefiniteException("参数错误");
		}
		String userId = params.get("userId")+"";
		Map<String, Object> user = userService.getUserById(Integer.parseInt(userId));
		int num = userService.getUserPriceToVipLevelNum(Integer.parseInt(userId));
		if(num > 0) {
			request.setAttribute("show","NO");
		} else {
			request.setAttribute("show","YES");
		}
		request.setAttribute("user", user);
		request.setAttribute("param", params);
		return "user/proxy/proxyPrice";

	}
	
	@RequestMapping("/setProxyPrice")
	@ResponseBody
	public ReturnBody setProxyPrice(@RequestParam Map<String, Object> params) {
		log.info("初始化代理价...");
		ReturnBody rb = ReturnBody.init();
		if (params == null || params.isEmpty()) {
			return rb.error("error", "参数不能为空");
		}
		return userService.saveProxyPrice(params);
	}

}
